#!/usr/bin/env python
#coding:utf-8
import os,sys,shutil
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import Settings
from werkzeug import secure_filename

class UpFile(object):
    def __init__(self):
        self.save_allow=Settings.set_flask_upallow

    def clear_up_dir(self,save_dir):
        # os.system('rm -rf {}*'.format(save_dir))
        shutil.rmtree(save_dir)
        os.mkdir(save_dir)
    def upload_file_check(self,filename):
        filename = secure_filename(filename)
        return '.' in filename and filename.rsplit('.', 1)[1] in self.save_allow
    def upload_exec(self,request_file_list,save_dir):
        up_files=[]
        self.clear_up_dir(save_dir)
        for file in request_file_list:
            filename=file.filename
            if file and self.upload_file_check(filename):
                file.save('{}{}'.format(save_dir,filename))
                up_files.append(filename)
            else:
                return 1,'{}该文件类型禁止上传'.format(filename)
        return 0,up_files